package com.dbappsecurity.bifrost.mgr;

import android.os.Handler;
import android.os.Message;
import cn.sharesdk.dingding.friends.Dingding;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import com.dbappsecurity.bifrost.entity.ShareContent;
import com.dbappsecurity.bifrost.enums.PlatformID;
import com.mob.tools.utils.UIHandler;
import java.util.HashMap;

import static cn.sharesdk.framework.Platform.SHARE_WEBPAGE;

public enum ShareMgr implements PlatformActionListener, Handler.Callback {

  INS;

  public interface ShareListener {
    void OnShareSuccess(Platform platform);

    void OnShareFail();

    void OnShareCancel();
  }

  ShareListener mShareListener;

  private static final int MSG_SHARE_COMPLETE = 4;
  private static final int MSG_SHARE_CANCEL = 5;
  private static final int MSG_SHARE_ERROR = 6;

  public void share(ShareContent content, ShareListener listener) {
    switch (content.platform) {
      case PlatformID.QQ:
        QQShare(content, listener);
        break;
      case PlatformID.WECHAT:
        WeChatShare(content, listener);
        break;
      case PlatformID.WECHATMOUNT:
        WechatMomentsShare(content, listener);
        break;
      case PlatformID.WEIBO:
        WeiboShare(content, listener);
        break;
      case PlatformID.QZONE:
        QZoneShare(content, listener);
        break;
      case PlatformID.DINGDING:
        DingShare(content, listener);
        break;
      default:
        break;
    }
  }

  private void WeChatShare(ShareContent content, ShareListener listener) {
    mShareListener = listener;
    Wechat.ShareParams sp = new Wechat.ShareParams();
    sp.setShareType(SHARE_WEBPAGE);
    sp.setTitle(content.title);
    sp.setText(content.content);
    sp.setImageUrl(content.image);
    sp.setTitleUrl(content.url);
    sp.setUrl(content.url);
    Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
    wechat.setPlatformActionListener(this); // 设置分享事件回调
    wechat.share(sp);
  }

  private void WechatMomentsShare(ShareContent content, ShareListener listener) {
    mShareListener = listener;
    WechatMoments.ShareParams sp = new WechatMoments.ShareParams();
    sp.setShareType(SHARE_WEBPAGE);
    sp.setTitle(content.title);
    sp.setText(content.content);
    sp.setImageUrl(content.image);
    sp.setTitleUrl(content.url);
    sp.setUrl(content.url);
    Platform moments = ShareSDK.getPlatform(WechatMoments.NAME);
    moments.setPlatformActionListener(this); // 设置分享事件回调
    moments.share(sp);
  }

  private void QQShare(ShareContent content, ShareListener listener) {
    mShareListener = listener;
    QQ.ShareParams sp = new QQ.ShareParams();
    sp.setImageUrl(content.image);
    sp.setTitle(content.title);
    sp.setTitleUrl(content.url);
    sp.setText(content.content);
    Platform qq = ShareSDK.getPlatform(QQ.NAME);
    qq.setPlatformActionListener(this); // 设置分享事件回调
    qq.share(sp);
  }

  private void DingShare(ShareContent content,ShareListener listener){
    mShareListener = listener;
    Dingding.ShareParams sp = new Dingding.ShareParams();
    sp.setShareType(SHARE_WEBPAGE);
    sp.setImageUrl(content.image);
    sp.setTitle(content.title);
    sp.setText(content.content);
    sp.setUrl(content.url);
    Platform dd = ShareSDK.getPlatform(Dingding.NAME);
    dd.setPlatformActionListener(this); // 设置分享事件回调
    dd.share(sp);
  }


  private void QZoneShare(ShareContent content, ShareListener listener) {
    mShareListener = listener;
    QZone.ShareParams sp = new QZone.ShareParams();
    sp.setImageUrl(content.image);
    sp.setTitle(content.title);
    sp.setTitleUrl(content.url);
    sp.setText(content.content);
    sp.setSite(content.title);
    sp.setSiteUrl(content.url);
    Platform qq = ShareSDK.getPlatform(QZone.NAME);
    qq.setPlatformActionListener(this); // 设置分享事件回调
    qq.share(sp);
  }

  private void WeiboShare(ShareContent content, ShareListener listener) {
    mShareListener = listener;
    SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
    sp.setImageUrl(content.image);
    sp.setText(content.toString());
    Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
    sinaWeibo.setPlatformActionListener(this); // 设置分享事件回调
    sinaWeibo.share(sp);
  }

  //回调实现
  @Override
  public void onComplete(Platform platform, int action, HashMap<String, Object> hashMap) {
    Message msg = Message.obtain();
    switch (action) {
      case Platform.ACTION_SHARE:
        msg.what = MSG_SHARE_COMPLETE;
        break;
      default:
        return;
    }
    UIHandler.sendMessage(msg, this);
  }

  @Override
  public void onError(Platform platform, int action, Throwable throwable) {
    Message msg = Message.obtain();
    switch (action) {
      case Platform.ACTION_SHARE:
        msg.what = MSG_SHARE_ERROR;
        break;
      default:
        return;
    }
    UIHandler.sendMessage(msg, this);
  }

  @Override
  public void onCancel(Platform platform, int action) {
    Message msg = Message.obtain();
    switch (action) {
      case Platform.ACTION_SHARE:
        msg.what = MSG_SHARE_CANCEL;
        break;
      default:
        return;
    }
    UIHandler.sendMessage(msg, this);
  }

  @Override
  public boolean handleMessage(Message msg) {
    switch (msg.what) {
      case MSG_SHARE_COMPLETE:
        if (mShareListener != null) {
          mShareListener.OnShareSuccess((Platform) msg.obj);
        }
        break;
      case MSG_SHARE_CANCEL:
        if (mShareListener != null) {
          mShareListener.OnShareCancel();
        }
        break;
      case MSG_SHARE_ERROR:
        if (mShareListener != null) {
          mShareListener.OnShareFail();
        }
        break;
      default:
        return false;
    }

    return true;
  }

}


